package com.xyy.controller;

import com.xyy.common.PageResult;
import com.xyy.common.Result;
import com.xyy.dto.DoctorCreateDTO;
import com.xyy.dto.DoctorQueryDTO;
import com.xyy.dto.DoctorUpdateDTO;
import com.xyy.entity.Doctor;
import com.xyy.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 医生管理控制器（管理员使用）
 */
@Slf4j
@RestController
@RequestMapping("/doctor-manage")
@Validated
public class DoctorManageController {

    @Autowired
    private DoctorService doctorService;

    /**
     * 创建医生（仅管理员）
     */
    @PostMapping("/create")
    public Result<Void> createDoctor(@Valid @RequestBody DoctorCreateDTO createDTO) {
        try {
            doctorService.createDoctor(createDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("创建医生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新医生信息（仅管理员）
     */
    @PutMapping("/update")
    public Result<Void> updateDoctor(@Valid @RequestBody DoctorUpdateDTO updateDTO) {
        try {
            doctorService.updateDoctor(updateDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("更新医生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 软删除医生（仅管理员）
     */
    @DeleteMapping("/delete/{doctorId}")
    public Result<Void> deleteDoctor(@PathVariable Long doctorId) {
        try {
            doctorService.deleteDoctor(doctorId);
            return Result.success();
        } catch (Exception e) {
            log.error("删除医生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量软删除医生（仅管理员）
     */
    @DeleteMapping("/batch-delete")
    public Result<Void> batchDeleteDoctor(@RequestBody List<Long> doctorIds) {
        try {
            doctorService.batchDeleteDoctor(doctorIds);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除医生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 恢复已删除的医生（仅管理员）
     */
    @PutMapping("/restore/{doctorId}")
    public Result<Void> restoreDoctor(@PathVariable Long doctorId) {
        try {
            doctorService.restoreDoctor(doctorId);
            return Result.success();
        } catch (Exception e) {
            log.error("恢复医生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页查询医生（仅管理员）
     */
    @PostMapping("/page")
    public Result<PageResult<Doctor>> pageQuery(@RequestBody DoctorQueryDTO queryDTO) {
        try {
            // 从queryDTO中获取分页参数
            Integer current = 1;
            Integer size = 10;
            
            if (queryDTO.getPageDTO() != null) {
                current = queryDTO.getPageDTO().getCurrent();
                size = queryDTO.getPageDTO().getSize();
            }
            
            // 验证分页参数
            if (current == null || current < 1) current = 1;
            if (size == null || size < 1) size = 10;
            
            PageResult<Doctor> result = doctorService.pageQuery(queryDTO, current, size);
            return Result.success(result);
        } catch (Exception e) {
            log.error("分页查询医生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页查询已删除的医生（仅管理员）
     */
    @GetMapping("/deleted/page")
    public Result<PageResult<Doctor>> pageQueryDeleted(@RequestParam(defaultValue = "1") Integer current,
                                                      @RequestParam(defaultValue = "10") Integer size) {
        try {
            PageResult<Doctor> result = doctorService.pageQueryDeleted(current, size);
            return Result.success(result);
        } catch (Exception e) {
            log.error("分页查询已删除医生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询医生详情（仅管理员）
     */
    @GetMapping("/detail/{doctorId}")
    public Result<Doctor> getDoctorDetail(@PathVariable Long doctorId) {
        try {
            Doctor doctor = doctorService.getById(doctorId);
            if (doctor == null) {
                return Result.error("医生不存在");
            }
            return Result.success(doctor);
        } catch (Exception e) {
            log.error("查询医生详情失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取已删除医生列表（仅管理员）
     */
    @GetMapping("/deleted/list")
    public Result<List<Doctor>> getDeletedDoctors() {
        try {
            List<Doctor> doctors = doctorService.getDeletedDoctors();
            return Result.success(doctors);
        } catch (Exception e) {
            log.error("获取已删除医生列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 统计已删除医生总数（仅管理员）
     */
    @GetMapping("/deleted/count")
    public Result<Long> countDeletedDoctors() {
        try {
            Long count = doctorService.countDeletedDoctors();
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计已删除医生总数失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
} 